import axios from "axios"
//组件外部使用编程式 
import {createBrowserHistory} from "history"

let history = createBrowserHistory()
let http = axios.create({
    baseURL: "/api",
    withCredentials: true,
    timeout: 1000*10
})

//请求拦截

http.interceptors.request.use(config => {
    //携带token
    // console.log(config);
    let token = JSON.parse(localStorage.getItem("qf-token") as string)

    config.headers!.authorization = token as string

    return config
})

//响应拦截
http.interceptors.response.use(config => {
    // console.log(config);
    if(config.data.code === "1004") {
        //校验失败 登录过期
        //清除token 跳转到登录页
        localStorage.removeItem("qf-token")
        history.push("/login")
        // window.location.reload()

    }
    
    return config
})

export default http
